﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace XChipTool
{
    public partial class XChip_ImgShow : DevExpress.XtraEditors.XtraForm
    {
        string ImgPath;
        public delegate void SendEventCadjImageClick(int x, int y, int w, int h);
        public event SendEventCadjImageClick SendCadjImage;
        private bool isClose = false;
        public XChip_ImgShow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 文件显示路劲
        /// </summary>
        /// <param name="path"></param>
        /// <param name="_isclose">是否必须框选才能关闭</param>
        public XChip_ImgShow(string path,bool _isclose =false) {
            ImgPath = path;
            isClose = _isclose;
             InitializeComponent();
        }

        private void XChip_ImgShow_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ImgPath))
            {
                var img = Image.FromFile(ImgPath);
                if (img.Width < 640)
                {
                    this.Width = img.Width + 2;
                    this.Height = img.Height + 32;
                }
                else if (img.Width < 880)
                {
                    this.Width = (int)(img.Width * 0.7 + 2);
                    this.Height = (int)(img.Height * 0.7 + 32);
                }
                else
                {
                    this.Width = (int)(img.Width * 0.5 + 2);
                    this.Height = (int)(img.Height * 0.5 + 32);
                }
                pictueImg.Image = img;
            }
        }
        #region 鼠标绘制
        bool fHaveImage = false;            //是否有图片
        Point fPtFirst;                     //鼠标按下去的位置
        Point fPtSecond;                    //鼠标当前位置
        bool blnDraw = false;
        Rectangle rect;
        /// <summary>
        ///  鼠标按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictueImg_MouseDown(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Cross;
            fPtFirst = new Point(e.X, e.Y);
            //fPtStart = new Point(e.X, e.Y);
            if ((sender as PictureEdit).Image != null)
            {
                this.fHaveImage = true;
                blnDraw = true;
            }
            else
            {
                this.fHaveImage = false;
                blnDraw = false;
            }
        }
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictueImg_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.Cursor == Cursors.Cross)
            {
                fPtSecond = new Point(e.X, e.Y);

                if (blnDraw)
                {
                    rect.Location = new Point(fPtFirst.X, fPtFirst.Y);
                    rect.Size = new Size(fPtSecond.X - fPtFirst.X, fPtSecond.Y - fPtFirst.Y);
                    pictueImg.Invalidate();
                }
            }
        }
        /// <summary>
        ///  鼠标抬起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictueImg_MouseUp(object sender, MouseEventArgs e)
        {
            blnDraw = false;//结束绘制
            if (this.fHaveImage == false) return;

            try
            {
                fPtSecond = new Point(e.X, e.Y);

                if (blnDraw)
                {
                    rect.Location = new Point(fPtFirst.X, fPtFirst.Y);
                    rect.Size = new Size(fPtSecond.X - fPtFirst.X, fPtSecond.Y - fPtFirst.Y);
                    //picEdit.Invalidate();
                }
                //Rectangle rectDisplay = this.GetPictureDisplaySize(picEdit);        //图片实际显示的大小


                int Width = Math.Abs((fPtSecond.X - fPtFirst.X));
                int Height = Math.Abs((fPtSecond.Y - fPtFirst.Y));

                double dWRate = pictueImg.Image.Width / (double)pictueImg.Size.Width;    //缩放比例：宽
                double dHRate = pictueImg.Image.Height / (double)pictueImg.Size.Height;  //缩放比例：高

                int intRealX = (int)(fPtFirst.X * dWRate); //实际的X坐标
                int intRealY = (int)(fPtFirst.Y * dHRate); //实际的Y坐标
                int intWidth = (int)(Width * dWRate);//实际的宽度
                int intHeight = (int)(Height * dWRate);//实际的高度
                if (null != SendCadjImage)
                {
                    SendCadjImage(intRealX, intRealY, intWidth, intHeight);
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { this.Cursor = Cursors.Default; }
        }
        /// <summary>
        /// 响应 重绘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictueImg_Paint(object sender, PaintEventArgs e)
        {
            if (this.fHaveImage)
            {
                if (rect != null && rect.Width > 0 && rect.Height > 0)
                {
                    e.Graphics.DrawRectangle(new Pen(Color.Yellow, 1), rect);//重新绘制颜色为红色
                }
            }
        }
        /// <summary>
        ///  鼠标离开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictueImg_MouseLeave(object sender, EventArgs e)
        {
            pictueImg.Invalidate();
        }
        #endregion

        /// <summary>
        /// 没有框选区域就不然关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XChip_ImgShow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isClose)
            {
                if (Math.Abs((fPtSecond.X - fPtFirst.X)) <= 0)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("请框选区域！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    e.Cancel = true;
                    return;
                }
            }
        }
    }
}